install.packages("RCurl")
require(RCurl)

year <- 2013
cityName <- "(Tokija, 35\u00B041\u2032N 139\u00B042\u2032E)"
timeZone <- "+9"


https://docs.google.com/spreadsheet/pub?key=0AgDon7xFOKEudHlNbzR5WV9OMktzS21DcF94cktHTlE&single=true&gid=0&output=csv

key <- "0AgDon7xFOKEudHlNbzR5WV9OMktzS21DcF94cktHTlE"
docParams <- "&single=true&gid=0&output=csv"
csvDataLocation <- paste("https://docs.google.com/spreadsheet/pub?key=",
                         key,docParams,sep="")
moonCSV <- getURL(csvDataLocation, ssl.verifypeer=FALSE)

moonFrame <- read.csv(textConnection(moonCSV))

hour <- function(x) {
  hrs <- x %/% 100
  return(hrs + (x - 100*hrs)/60)
}

days<-c(31,28+(year %% 4 == 0),31,30,31,30,31,31,30,31,30,31)

moonRises <- c()
moonSets <- c()
for (i in 1:12) {
  moonRises <- c(moonRises,sapply(moonFrame[,2*i][1:days[i]],hour))
  moonSets<- c(moonSets,sapply(moonFrame[,2*i+1][1:days[i]],hour))
}


x <- 1:sum(days)
windows.options(width=15, height=10)
plot(x, type="n", asp=8, xaxs="i", yaxs="i", ylim=c(0,24), 
     axes=FALSE, ann=FALSE, xpd=NA)

cd <- c(0,cumsum(days))
for (m in 1:12) {
  if (m %% 2 == 1) {
    polygon(c(cd[m],cd[m],cd[m+1],cd[m+1]),
            c(0,24,24,0),col='lightskyblue1', border=NA)
  }
  else {
    polygon(c(cd[m],cd[m],cd[m+1],cd[m+1]),
            c(0,24,24,0),col='beige', border=NA)
  }
}

abline(h=0:24, col="lightgray")
points(x, moonRises, type="l", col="red", lwd=1)
points(x, moonSets, type="l", col="blue", lwd=1)

# Compute moon's time above the horizon
above <- c()
for (jj in x) {
  if (is.na(moonRises[jj]) && is.na(moonSets[jj])) {
    above <- c(above, NA)
  }
  else if (is.na(moonRises[jj]) && !is.na(moonSets[jj])) {
    above <- c(above, moonSets[jj])
  }
  else if (!is.na(moonRises[jj]) && is.na(moonSets[jj])) {
    above <- c(above, 24 - moonRises[jj])
  }  
  else if (moonSets[jj] > moonRises[jj]) {
    above <- c(above,moonSets[jj] - moonRises[jj])
  }
  else if (moonSets[jj] < moonRises[jj]) {
    above <- c(above, moonSets[jj] + (24 - moonRises[jj]))
  }
}
points(above,type="l",lwd="2",col="chartreuse3")


axis(1, at=(cd+1)[1:12], las=1, lab=paste('01',
  c("01","02","03","04","05","06","07","08","09","10","11","12"),sep="."))
axis(2, las=1, at=3*0:8)

title(main=paste("M\u0113ness un laiks virs horizonta",cityName), col.main="red")
title(xlab=paste(year,". gads", sep=""), col.lab=rgb(0,0.5,0))
title(ylab=paste("Viet\u0113jais laiks: UTC",timeZone,sep=""), col.lab=rgb(0,0.5,0))
box()
